<?php
include './include/common.inc.php';
include './include/menu.class.php';
require '../include/form.class.php';
$menu = new menu;
$action = isset($action) ? $action : 'manage';
$forward = get_forward( 'menu.php' );

$params = array();
array_push( $params, "status = 99" );

$siteid = get_current_site();
$conf = $site_conf->get_site_conf();
$site = new site();
$site_info = $site->get_one($siteid);
if($conf['IS_MASTER'] != 1)
{
	array_push( $params, "parentid = '{$site_info['menusite']}'" );
	array_push( $params, "siteid = '{$siteid}'" );
}
else 
{
	array_push( $params, "parentid = '0'" );
}
$order = ' order by listorder desc, id desc';
//检索用的siteid
$smid = $conf['IS_MASTER'] == 1 ? false : $siteid;
if( $current_user === false )
{
	if( isset( $edufepm ) && $edufepm == 'iframe' )
	{
		$response->alert( '请您登陆后再访问' );
		$response->redirect( $base_dir . 'admin/login.php' );
		$response->execute();
	}
	else
	{
		showmessage( '请您登陆后再访问', $base_dir . 'admin/login.php' );
	}
}

switch($action)
{
    case 'get_menu_list' :
        $canview = ( $current_user['usertype'] == USER_TYPE_SUPER ) ? true : get_view_resource( 'menu' );
        $data = $menu->get_submenu($menuid, $canview, $smid);
        if(is_array($data))
        {
        	foreach ($data as &$v)
        	{
        		if($v['siteid'] != $siteid)
        		{
        			if(stristr($v['url'],'.php?'))
        			{
        				$v['url'] .= '&site_id='.$v['siteid']; 
        			}
        			else 
        			{
        				$v['url'] .= '?site_id='.$v['siteid']; 
        			}
        		}
        	}
        }
        echo json_encode($data);
        break;

    case 'add':
        $have_perm = check_perms( 'menu002' );
        if( isset( $_POST['name'] ) )
		{
            if( $have_perm === false )
			{
				$response->alert( '您没有权限进行此操作！' );
				$response->execute();
			}
			if( empty( $name ) || strlen( $name ) > 200 )
			{
				$response->text_alert( 'error','name','菜单名称不能为空且长度不能大于200个字符' );
			}
            $check_name = $menu->check_name( $parentid, $name );
            if( !empty($check_name) )
            {
                $response->text_alert( 'error','name','菜单名称已存在' );
            }
			$response->real_execute();
			$params = array(
				'name'	      =>  $name,
                'parentid'    =>  $parentid,
                'siteid'	  =>  $siteid,
                'url'         =>  $url,
                'isfolder'    =>  0,
                'isopen'      =>  isset($isopen) ? $isopen : 0 ,
                'image'       =>  $image,
                'status'      =>  99,
			);
            $res = $menu->add( $params );
            if( $parentid )
            {
                $menu->update_isfolder_status( $parentid );
            }
			if( !empty ($res) )
			{
				$response->alert( '添加成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '添加失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
            $menuid = isset($menuid) ? $menuid : 1;
			$info = $menu->get( $menuid , $smid);
            if( isset( $info['parentid'] ) )
			{
                if( strlen($info['parentid']) == 36 )
                {
                    $parent = $menu->get( $menuid );
                    $parentname = $parent['name'];
                    $parentid = $parent['id'];
                }
			}
            $parent_forward = make_forward( $forward );
			include template( 'admin', 'menu_add' );
		}
        break;
    
    case 'edit' :
        $have_perm = check_perms( 'menu003' );
        if( isset( $_POST['name'] ) )
		{
            if( $have_perm === false )
			{
				$response->alert( '您没有权限进行此操作！' );
				$response->execute();
			}
            if( empty( $name ) || strlen( $name ) > 200 )
			{
				$response->text_alert( 'error','name','菜单名称不能为空且长度不能大于200个字符' );
			}
            $r = $menu->get( $menuid, $smid );
            if( $r['name'] != $name )
            {
                $check_name = $menu->check_name( $parentid, $name );
            }
            if( !empty($check_name) )
            {
                $response->text_alert( 'error','name','菜单名称已存在' );
            }
			$response->real_execute();
            if(empty($parentid))//判断是否为空，为空则没有改变
            {
                $parentid = $r['parentid'];//parentid为空，值无改变，数据库的父ID赋值
                $isfolder = $r['isfolder'];//文件夹属性不变
            }
            //修改上级菜单类型
            if( $r['parentid'] != $parentid && !empty($parentid) )//判断值是否改变且parentid必须不为空
            {
                if( $r['parentid'] ) $menu->update_isfolder_status( $r['parentid'] );
                if( $parentid )
                {
                    $menu->update( $parentid, array( 'isfolder'=>1 ) );
                }
            }
            //修改菜单参数
            $params = array(
				'name'	      =>  $name,
                'parentid'    =>  $parentid,
                'url'         =>  $url,
                'image'       =>  $image,
                'isfolder'    =>  $isfolder,
                'isopen'      =>  isset($isopen) ? $isopen : 0,
                'status'      =>  99,
			);
			$res = $menu->update( $menuid, $params );
			if( !empty($res) )
			{
				$response->alert( '修改成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '修改失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
			$info = $menu->get( $menuid, $smid );
			if( !isset( $info['id'] ) )
			{
				showmessage( '要编辑的菜单不存在' );
			}
            if( strlen($info['parentid']) == 36 )
			{
                $parent = $menu->get( $info['parentid'] );
				$parentname = $parent['name'];
			}
			else
			{
				$parentname = '无';
			}
            if( isset( $reset ) && $reset == 'yes' )
            {
                $parentname = '';
            }
            $parent_forward = make_forward( $forward );
			include template( 'admin', 'menu_upd' );
		}
        break;

    case 'del':
        $have_perm = check_perms( 'menu004' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
		$info = $menu->get( $menuid, $smid );
		if( !isset( $info['id'] ) )
		{
			$response->alert( '要删除的菜单不存在' );
			$response->execute();
		}
		$res = $menu->delete( $menuid );
		if( $res == false )
		{
			$response->alert( '删除失败' );
		}
		else
		{
			$response->alert( '删除成功' );
			$response->script( 'self.location=self.location' );
		}
		$response->execute();
        break;

    case 'listorder':
        $have_perm = check_perms( 'menu005' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        $result = $menu->listorder( $listorder );
		if( $result )
		{
            $response->alert( '修改成功' );
            $response->script( 'self.location=self.location' );
		}
		else
		{
			$response->alert( '修改失败' );
		}
        $response->execute();
        break;

    case 'getchild':
		$array = array();
        $params = array();
        $parentid = isset($parentid) ? $parentid : 0 ;
        array_push( $params, "status = 99" );
        array_push( $params, "parentid='$parentid'" );
        if($conf['IS_MASTER'] != 1)
		{
			array_push( $params, "siteid = '{$siteid}'" );
		}
        $infos = $menu->get_list($params, $order, $canview='');
	    foreach($infos as $k=>$v)
	    {
			$array[$v['id']] = $v['name'];
	    }
		if(!$parentid || $array)
	    {
			$array[0] = $parentid ? '请选择' : '无';
			ksort($array);
			echo form::select($array, 'setparentid', 'setparentid', $parentid, 'onchange="if(this.value>0){getchild(this.value);myform.parentid.value=this.value;this.disabled=true;}"');
		}
		break;

    case 'child':
        $have_perm = check_perms( 'menu001' );
        if( $have_perm === false )
        {
            showmessage( '您没有权限进行此操作！');
        }
        $params = array();
        isset( $menuid ) ? $menuid : 0;
        array_push( $params, "status = 99" );
        array_push( $params, "parentid = '$menuid'" );
        if($conf['IS_MASTER'] != 1)
		{
			array_push( $params, "siteid = '{$siteid}'" );
		}
        $page = isset( $page ) ? intval( $page ) : 1;
		$page = max( $page, 1 );
        $pagesize = 10;
        $canview = ( $current_user['usertype'] == USER_TYPE_SUPER ) ? true : get_view_resource( 'menu' );
        $menu_list = $menu->get_list( $params, $order, $canview, $page, $pagesize );
        $menu_pages = $menu->pages;
        $child = 'child';
        $parent_forward = $forward;
        $forward = make_forward();
		include template( 'admin', 'menu' );
        break;

    case 'manage':

    default :
        $have_perm = check_perms( 'menu001' );
        if( $have_perm === false )
        {
            showmessage( '您没有权限进行此操作！');
        }
        $page = isset( $page ) ? intval( $page ) : 1;
		$page = max( $page, 1 );
        $pagesize = 10;
        $canview = ( $current_user['usertype'] == USER_TYPE_SUPER ) ? true : get_view_resource( 'menu' );
		$menu_list = $menu->get_list( $params, $order, $canview, $page, $pagesize );
        $menu_pages = $menu->pages;
        $forward = make_forward();
		include template( 'admin', 'menu' );
        break;
}
?>
